package com.ww.springboot.boot.algorithm.leetcode1;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-26 10:03
 */
public class BV883三维形体投影面积 {

    public static void main(String[] args) {

        int[][] grid = {{1, 2}, {3, 4}};
        System.out.println(projectionArea(grid));

    }

    public static int projectionArea(int[][] grid) {
        int x = grid.length;
        int y = grid[0].length;
        int total = 0;
        //底面积
        int bottomArea = 0;
        //前面积
        int[] frontArea = new int[x];
        //侧面积
        int[] sideArea = new int[y];


        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (grid[i][j] > 0) {
                    bottomArea++;
                }
                if (sideArea[i] < grid[i][j]) {
                    sideArea[i] = grid[i][j];
                }
                if (frontArea[i] < grid[j][i]) {
                    frontArea[i] = grid[j][i];
                }
            }
        }

        int front = 0;
        for (int i = 0; i < frontArea.length; i++) {
            front += frontArea[i];
        }
        int side = 0;
        for (int i = 0; i < sideArea.length; i++) {
            side += sideArea[i];
        }
        total += bottomArea;
        total += front;
        total += side;
        return total;
    }
}
